<template>
        <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="200px" size="medium">
            <el-form-item label="保司名称" prop="name">
                <el-input type="text" v-model="ruleForm.name" class="w300"></el-input>
            </el-form-item>
            <el-form-item label="保司状态" prop="status">
                <el-select v-model="ruleForm.status" class="w300">
                    <el-option v-for="item in cOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="合作主机厂" prop="twList">
                <el-checkbox-group v-model="ruleForm.twList" class="w180">
                    <el-checkbox v-for="item in cCheckList" :key="item" :label="item">{{item}}</el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item label="管理员" prop="userName">
                <el-input type="text" v-model="ruleForm.userName" class="w300"></el-input>
            </el-form-item>
            <el-form-item label="管理员账号" prop="userAccount">
                <el-input type="text" v-model="ruleForm.userAccount" class="w300"></el-input>
            </el-form-item>
            <div class="mb20 pdl200">注：初始密码为666666</div>
            <el-form-item>
                <el-button type="primary" @click="onSubmit('ruleForm')">确 定</el-button>
                <el-button @click="onCancel()">取 消</el-button>
            </el-form-item>
        </el-form>
</template>

<script>
	export default {
        name: 'editInsurance',
		data () {
			return {
                cOptions: [
                    { value: '在册', label: '在册' },
                    { value: '注销', label: '注销' }
                ],
                flag : false,
                cCheckList: ['吉利汽车','长城汽车','东风汽车','本田汽车','恒大汽车'],
                ruleForm: {
                    index: 0,
                    name: '',
                    status: '',
                    twList: [],
                    userName: '',
                    userAccount: ''
                },
                rules: {
                    name: [
                        { required: true, message: '请输入保司名称', trigger: 'blur' }
                    ],
                    status: [
                        { required: true, message: '请选择保司状态', trigger: 'change' }
                    ],
                    twList: [
                        { type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
                    ],
                    userName: [
                        { required: true, message: '请输入管理员名称', trigger: 'blur' }
                    ],
                    userAccount: [
                        { required: true, message: '请输入管理员账号', trigger: 'blur' }
                    ]
                }
            }
		},
		created () {
            if (this.$state.query && this.$state.query.insuranceData) {
                this.flag = true,
                this.ruleForm = {
                    ...this.$state.query.insuranceData
                }
            }
		},
        mounted() {
            
        },
		methods: {
            
            // request () {
            //     let params = { ...this.form, userId: this.$store.state.userInfo.user.userId }
            //     ssoService.updatePassword(params).then(() => {
            //         this.close()
            //         this.openMessage()
            //     }).catch((err) => {
            //         this.$message.error(err.message)
            //     })
            // },
            // openMessage () {
            //     this.$alert('密码修改成功！', '提示', {
            //         confirmButtonText: '确定',
            //         callback: action => {
            //             setTimeout(() => {
            //                 this.$router.replace('login')
            //             }, 0)
            //         }
            //     })
            // },
            onSubmit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        // alert('submit!');
                        // this.$store.dispatch('updataInsurance',this.ruleForm);
                        let lsData = JSON.parse(localStorage.getItem('insuranceData'));
                        if(this.flag){
                            lsData[this.ruleForm.index - 1].name = this.ruleForm.name;
                            lsData[this.ruleForm.index - 1].status = this.ruleForm.status;
                            lsData[this.ruleForm.index - 1].twList = this.ruleForm.twList;
                        }else{
                            this.ruleForm.total = 0;
                            this.ruleForm.bxnums = 0;
                            this.ruleForm.index = lsData.length + 1;
                            lsData.push(this.ruleForm);
                        }
                        localStorage.setItem('insuranceData', JSON.stringify(lsData));
                        this.$message.success('修改成功!')
                        this.$state.query.callback()
                        // this.resetSetItem('insuranceData', JSON.stringify(lsData));
                        this.$modal.close()
                        // this.$routerTo('insuranceInfo');
                    } else {
                        return false;
                    }
                });
            },
            onCancel () {
                this.$modal.close();
            }
        }
	}
</script>

<style scoped>
    .w180{
        width: 180px;
    }
    .w300{
        width: 300px;
    }
</style>
